package com.ruoyi.file.service.impl;

import com.ruoyi.file.service.ISysFileFormatService;
import com.ruoyi.file.controller.SysFileFormatController;
import com.ruoyi.file.utils.CscpFileToPdfConverter;
import com.ruoyi.file.utils.CscpOSHelper;
import com.ruoyi.file.utils.VaipFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.*;

/**
 * Cscp system file format service.
 *
 * @author william.zhao
 * @version 1.0
 * @date 2024/06/11
 * @since jdk-17.0.2
 */
@Primary
@Service
public class SysFileFormatServiceImpl implements ISysFileFormatService {
    private static final Logger log = LoggerFactory.getLogger(SysFileFormatController.class);

    /** the suffix of pdf.(.pdf) */
    private String SUFFIX_PDF = ".pdf";
    /** the vaip file util. */
    private VaipFileUtil fileUtil = new VaipFileUtil();


    /**
     * format file.
     *
     * @param filePath the relative file path in server.
     * @return the formatted pdf file path in server;
     * @throws Exception if any exceptions happened, throw them.
     */
    public String formatFile(String filePath, String targetFormat) throws Exception {
        String reviewFilePath = null;
        String suffix = fileUtil.getSuffix(filePath);
        if (targetFormat.equalsIgnoreCase("pdf") || targetFormat.equalsIgnoreCase(".pdf")) {
            if (suffix.equals(".doc")
                    || suffix.equals(".docx")
                    || suffix.equals(".ppt")
                    || suffix.equals(".pptx")
                    || suffix.equals(".xlsx")
                    || suffix.equals(".xls")
                    || suffix.equals(".txt")) {
                reviewFilePath = this.processDocToPdf(filePath);
            }
        }

        return reviewFilePath;
    }

    /**
     * convert doc file to PDF.
     *
     * @param filePath the relative file path in server.
     * @return the formatted pdf file path in server;
     * @throws Exception if any exceptions happened, throw them.
     */
    private String processDocToPdf(String filePath) throws Exception {
        File docFile = new File(filePath);
        if (!docFile.exists()) {
            return null;
        }

        log.info("processDocToPdf()-->{}", filePath);
        String pdfFilePath = fileUtil.changeTo(filePath, SUFFIX_PDF);
        InputStream inputStream = new FileInputStream(docFile);

        if (CscpOSHelper.isWindows()) {
            OutputStream outputStream = new FileOutputStream(pdfFilePath);
            CscpFileToPdfConverter.converterDocToPdf(inputStream, outputStream);
        } else {
            String fileDirPath = docFile.getParent();
            CscpFileToPdfConverter.convertLinuxFileToPdf(filePath, fileDirPath);
        }

        log.info("processed to {}", pdfFilePath);
        // format fail!
        if (!new File(pdfFilePath).exists()) {
            return null;
        }

        return pdfFilePath;
    }

}
